{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8a299027-038e-4059-b0e4-578701b1477d",
   "metadata": {},
   "outputs": [],
   "source": [
    "import warnings\n",
    "\n",
    "warnings.filterwarnings(\"ignore\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "46e8d233",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patent_id</th>\n",
       "      <th>sentence_id</th>\n",
       "      <th>head</th>\n",
       "      <th>relation</th>\n",
       "      <th>tail</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10001020</td>\n",
       "      <td>10001020_0</td>\n",
       "      <td>present invention</td>\n",
       "      <td>generally relates to</td>\n",
       "      <td>fans</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10001020</td>\n",
       "      <td>10001020_0</td>\n",
       "      <td>present invention</td>\n",
       "      <td>generally relates to</td>\n",
       "      <td>fan assemblies</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10001020</td>\n",
       "      <td>10001020_0</td>\n",
       "      <td>present invention</td>\n",
       "      <td>generally relates to</td>\n",
       "      <td>fan wheels</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10001020</td>\n",
       "      <td>10001020_0</td>\n",
       "      <td>present invention</td>\n",
       "      <td>generally relates to</td>\n",
       "      <td>composite fan blades</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>10001020</td>\n",
       "      <td>10001020_0</td>\n",
       "      <td>present invention</td>\n",
       "      <td>generally relates</td>\n",
       "      <td>attachment</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2927524</th>\n",
       "      <td>9995315</td>\n",
       "      <td>9995315_37</td>\n",
       "      <td>wire winding space</td>\n",
       "      <td>is increased for</td>\n",
       "      <td>convenient assembling</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2927525</th>\n",
       "      <td>9995315</td>\n",
       "      <td>9995315_37</td>\n",
       "      <td>stator</td>\n",
       "      <td>in</td>\n",
       "      <td>motor housing</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2927526</th>\n",
       "      <td>9995315</td>\n",
       "      <td>9995315_37</td>\n",
       "      <td>stator</td>\n",
       "      <td>in</td>\n",
       "      <td>cover</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2927527</th>\n",
       "      <td>9995315</td>\n",
       "      <td>9995315_37</td>\n",
       "      <td>cover</td>\n",
       "      <td>of</td>\n",
       "      <td>ceiling fan motor</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2927528</th>\n",
       "      <td>9995315</td>\n",
       "      <td>9995315_37</td>\n",
       "      <td>cover</td>\n",
       "      <td>is increased for</td>\n",
       "      <td>convenient assembling</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>2927529 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        patent_id sentence_id                head              relation  \\\n",
       "0        10001020  10001020_0   present invention  generally relates to   \n",
       "1        10001020  10001020_0   present invention  generally relates to   \n",
       "2        10001020  10001020_0   present invention  generally relates to   \n",
       "3        10001020  10001020_0   present invention  generally relates to   \n",
       "4        10001020  10001020_0   present invention     generally relates   \n",
       "...           ...         ...                 ...                   ...   \n",
       "2927524   9995315  9995315_37  wire winding space      is increased for   \n",
       "2927525   9995315  9995315_37              stator                    in   \n",
       "2927526   9995315  9995315_37              stator                    in   \n",
       "2927527   9995315  9995315_37               cover                    of   \n",
       "2927528   9995315  9995315_37               cover      is increased for   \n",
       "\n",
       "                          tail  \n",
       "0                         fans  \n",
       "1               fan assemblies  \n",
       "2                   fan wheels  \n",
       "3         composite fan blades  \n",
       "4                   attachment  \n",
       "...                        ...  \n",
       "2927524  convenient assembling  \n",
       "2927525          motor housing  \n",
       "2927526                  cover  \n",
       "2927527      ceiling fan motor  \n",
       "2927528  convenient assembling  \n",
       "\n",
       "[2927529 rows x 5 columns]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "\n",
    "#Importing facts\n",
    "\n",
    "facts = pd.read_pickle(\"data/fan-systems-facts.pkl\")\n",
    "facts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "25bca651-3621-431a-824c-a171eeefc09e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patent_id</th>\n",
       "      <th>sentence_id</th>\n",
       "      <th>head</th>\n",
       "      <th>relation</th>\n",
       "      <th>tail</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>24390</th>\n",
       "      <td>10036400</td>\n",
       "      <td>10036400_62</td>\n",
       "      <td>noise</td>\n",
       "      <td>generated by</td>\n",
       "      <td>airflow</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>52431</th>\n",
       "      <td>10072670</td>\n",
       "      <td>10072670_48</td>\n",
       "      <td>noises</td>\n",
       "      <td>are increased</td>\n",
       "      <td>swirling airflow</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>58343</th>\n",
       "      <td>10079525</td>\n",
       "      <td>10079525_194</td>\n",
       "      <td>heated airflows</td>\n",
       "      <td>the</td>\n",
       "      <td>noise sources</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>60152</th>\n",
       "      <td>10079526</td>\n",
       "      <td>10079526_194</td>\n",
       "      <td>heated airflows</td>\n",
       "      <td>the</td>\n",
       "      <td>noise sources</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>76943</th>\n",
       "      <td>10100846</td>\n",
       "      <td>10100846_43</td>\n",
       "      <td>airflow</td>\n",
       "      <td>to</td>\n",
       "      <td>noise production</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2768809</th>\n",
       "      <td>9745998</td>\n",
       "      <td>9745998_0</td>\n",
       "      <td>turbulent flow noise</td>\n",
       "      <td>caused by</td>\n",
       "      <td>airflow turbulence</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2820812</th>\n",
       "      <td>9850907</td>\n",
       "      <td>9850907_16</td>\n",
       "      <td>airflow</td>\n",
       "      <td>generates</td>\n",
       "      <td>noise</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2854495</th>\n",
       "      <td>9909485</td>\n",
       "      <td>9909485_28</td>\n",
       "      <td>recirculating airflow</td>\n",
       "      <td>to reduce</td>\n",
       "      <td>noise</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2854753</th>\n",
       "      <td>9909485</td>\n",
       "      <td>9909485_93</td>\n",
       "      <td>noise</td>\n",
       "      <td>in</td>\n",
       "      <td>certain airflow condition</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2881805</th>\n",
       "      <td>9945390</td>\n",
       "      <td>9945390_58</td>\n",
       "      <td>noise levels</td>\n",
       "      <td>to reduce</td>\n",
       "      <td>airflow restrictions</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>164 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        patent_id   sentence_id                   head       relation  \\\n",
       "24390    10036400   10036400_62                  noise   generated by   \n",
       "52431    10072670   10072670_48                 noises  are increased   \n",
       "58343    10079525  10079525_194        heated airflows            the   \n",
       "60152    10079526  10079526_194        heated airflows            the   \n",
       "76943    10100846   10100846_43                airflow             to   \n",
       "...           ...           ...                    ...            ...   \n",
       "2768809   9745998     9745998_0   turbulent flow noise      caused by   \n",
       "2820812   9850907    9850907_16                airflow      generates   \n",
       "2854495   9909485    9909485_28  recirculating airflow      to reduce   \n",
       "2854753   9909485    9909485_93                  noise             in   \n",
       "2881805   9945390    9945390_58           noise levels      to reduce   \n",
       "\n",
       "                              tail  \n",
       "24390                      airflow  \n",
       "52431             swirling airflow  \n",
       "58343                noise sources  \n",
       "60152                noise sources  \n",
       "76943             noise production  \n",
       "...                            ...  \n",
       "2768809         airflow turbulence  \n",
       "2820812                      noise  \n",
       "2854495                      noise  \n",
       "2854753  certain airflow condition  \n",
       "2881805       airflow restrictions  \n",
       "\n",
       "[164 rows x 5 columns]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Finding facts that mention 'airflow' and 'noise' in either of the entities\n",
    "\n",
    "issue = \"airflow noise\"\n",
    "level_0 = facts.loc[(facts[\"head\"] + \" \" + facts[\"tail\"]).str.contains(r'^(?=.*airflow)(?=.*noise)')]\n",
    "level_0"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "a16af0e1-03b5-49ad-9c53-86da133c31b9",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patent_id</th>\n",
       "      <th>sentence_id</th>\n",
       "      <th>head</th>\n",
       "      <th>relation</th>\n",
       "      <th>tail</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>24388</th>\n",
       "      <td>10036400</td>\n",
       "      <td>10036400_62</td>\n",
       "      <td>flow channel</td>\n",
       "      <td>facilitates reducing</td>\n",
       "      <td>noise</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24389</th>\n",
       "      <td>10036400</td>\n",
       "      <td>10036400_62</td>\n",
       "      <td>flow channel</td>\n",
       "      <td>facilitates reducing</td>\n",
       "      <td>airflow disruption</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24390</th>\n",
       "      <td>10036400</td>\n",
       "      <td>10036400_62</td>\n",
       "      <td>noise</td>\n",
       "      <td>generated by</td>\n",
       "      <td>airflow</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24391</th>\n",
       "      <td>10036400</td>\n",
       "      <td>10036400_62</td>\n",
       "      <td>noise</td>\n",
       "      <td>impacting</td>\n",
       "      <td>cabinet</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24392</th>\n",
       "      <td>10036400</td>\n",
       "      <td>10036400_62</td>\n",
       "      <td>airflow</td>\n",
       "      <td>impacting</td>\n",
       "      <td>cabinet</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2881802</th>\n",
       "      <td>9945390</td>\n",
       "      <td>9945390_58</td>\n",
       "      <td>mounting arm</td>\n",
       "      <td>is contoured to reduce</td>\n",
       "      <td>noise levels</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2881803</th>\n",
       "      <td>9945390</td>\n",
       "      <td>9945390_58</td>\n",
       "      <td>mounting arm</td>\n",
       "      <td>is contoured to reduce</td>\n",
       "      <td>airflow restrictions</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2881804</th>\n",
       "      <td>9945390</td>\n",
       "      <td>9945390_58</td>\n",
       "      <td>noise levels</td>\n",
       "      <td>generated by</td>\n",
       "      <td>blower</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2881805</th>\n",
       "      <td>9945390</td>\n",
       "      <td>9945390_58</td>\n",
       "      <td>noise levels</td>\n",
       "      <td>to reduce</td>\n",
       "      <td>airflow restrictions</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2881806</th>\n",
       "      <td>9945390</td>\n",
       "      <td>9945390_58</td>\n",
       "      <td>airflow restrictions</td>\n",
       "      <td>about</td>\n",
       "      <td>inlet opening</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>1131 rows × 5 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "        patent_id  sentence_id                  head                relation  \\\n",
       "24388    10036400  10036400_62          flow channel    facilitates reducing   \n",
       "24389    10036400  10036400_62          flow channel    facilitates reducing   \n",
       "24390    10036400  10036400_62                 noise            generated by   \n",
       "24391    10036400  10036400_62                 noise               impacting   \n",
       "24392    10036400  10036400_62               airflow               impacting   \n",
       "...           ...          ...                   ...                     ...   \n",
       "2881802   9945390   9945390_58          mounting arm  is contoured to reduce   \n",
       "2881803   9945390   9945390_58          mounting arm  is contoured to reduce   \n",
       "2881804   9945390   9945390_58          noise levels            generated by   \n",
       "2881805   9945390   9945390_58          noise levels               to reduce   \n",
       "2881806   9945390   9945390_58  airflow restrictions                   about   \n",
       "\n",
       "                         tail  \n",
       "24388                   noise  \n",
       "24389      airflow disruption  \n",
       "24390                 airflow  \n",
       "24391                 cabinet  \n",
       "24392                 cabinet  \n",
       "...                       ...  \n",
       "2881802          noise levels  \n",
       "2881803  airflow restrictions  \n",
       "2881804                blower  \n",
       "2881805  airflow restrictions  \n",
       "2881806         inlet opening  \n",
       "\n",
       "[1131 rows x 5 columns]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#shortlisting all facts that occur in the sentences of level 0\n",
    "\n",
    "facts = facts.loc[facts[\"sentence_id\"].isin(level_0[\"sentence_id\"])]\n",
    "facts"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "d511bcf0-107a-4c6f-ae98-e11f65c034f8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patent_id</th>\n",
       "      <th>head</th>\n",
       "      <th>relation</th>\n",
       "      <th>tail</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>90</th>\n",
       "      <td>6148954</td>\n",
       "      <td>apparatus</td>\n",
       "      <td>for reducing</td>\n",
       "      <td>airflow noise</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>11</th>\n",
       "      <td>10316862</td>\n",
       "      <td>lower pressure airflow</td>\n",
       "      <td>generates</td>\n",
       "      <td>less noise</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>112</th>\n",
       "      <td>7214033</td>\n",
       "      <td>frequency range</td>\n",
       "      <td>of</td>\n",
       "      <td>airflow noise</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>63</th>\n",
       "      <td>11397013</td>\n",
       "      <td>airflow</td>\n",
       "      <td>reduce</td>\n",
       "      <td>noise</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>6193011</td>\n",
       "      <td>apparatus</td>\n",
       "      <td>for reducing</td>\n",
       "      <td>airflow noise</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>157</th>\n",
       "      <td>8608439</td>\n",
       "      <td>airflow</td>\n",
       "      <td>at</td>\n",
       "      <td>lowest operational noise levels</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>45</th>\n",
       "      <td>11286953</td>\n",
       "      <td>noise</td>\n",
       "      <td>caused by</td>\n",
       "      <td>airflow</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>76</th>\n",
       "      <td>5577888</td>\n",
       "      <td>noise</td>\n",
       "      <td>generated by</td>\n",
       "      <td>airflow generator</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>38</th>\n",
       "      <td>11125238</td>\n",
       "      <td>airflow noise</td>\n",
       "      <td>of</td>\n",
       "      <td>wake</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>93</th>\n",
       "      <td>6193011</td>\n",
       "      <td>above mentioned attributes</td>\n",
       "      <td>is capable of reducing</td>\n",
       "      <td>airflow noise</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>141 rows × 4 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "    patent_id                        head                relation  \\\n",
       "90    6148954                   apparatus            for reducing   \n",
       "11   10316862      lower pressure airflow               generates   \n",
       "112   7214033             frequency range                      of   \n",
       "63   11397013                     airflow                  reduce   \n",
       "96    6193011                   apparatus            for reducing   \n",
       "..        ...                         ...                     ...   \n",
       "157   8608439                     airflow                      at   \n",
       "45   11286953                       noise               caused by   \n",
       "76    5577888                       noise            generated by   \n",
       "38   11125238               airflow noise                      of   \n",
       "93    6193011  above mentioned attributes  is capable of reducing   \n",
       "\n",
       "                                tail  \n",
       "90                     airflow noise  \n",
       "11                        less noise  \n",
       "112                    airflow noise  \n",
       "63                             noise  \n",
       "96                     airflow noise  \n",
       "..                               ...  \n",
       "157  lowest operational noise levels  \n",
       "45                           airflow  \n",
       "76                 airflow generator  \n",
       "38                              wake  \n",
       "93                     airflow noise  \n",
       "\n",
       "[141 rows x 4 columns]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#filtering the facts from the shortlist that only extend from level_o\n",
    "\n",
    "entity_markers = (level_0[\"patent_id\"] + \": \" + level_0[\"head\"]).tolist() + (level_0[\"patent_id\"] + \": \" + level_0[\"tail\"]).tolist()\n",
    "\n",
    "facts = facts.loc[(facts[\"patent_id\"] + \": \" + facts[\"head\"]).isin(entity_markers) & (facts[\"patent_id\"] + \": \" + facts[\"tail\"]).isin(entity_markers)]\n",
    "facts = facts.drop(columns=[\"sentence_id\"]).reset_index(drop=True)\n",
    "facts = facts.drop_duplicates()\n",
    "facts.sample(frac=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "20cefeae-b0a0-4942-99db-cf6582f04890",
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "#shuffling the facts\n",
    "facts[\"facts\"] = facts[\"head\"] + \" :: \" + facts[\"relation\"] + \" :: \" + facts[\"tail\"]\n",
    "fact_list = facts[\"facts\"].tolist()\n",
    "random.shuffle(fact_list)\n",
    "\n",
    "#getting the list of facts as text file\n",
    "with open(\"data/airflow-facts.txt\", \"w+\", encoding=\"utf-8\") as f:\n",
    "    f.write(\"\\n\".join(fact_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "334fdf1c-a52e-4d22-850e-545ca279f660",
   "metadata": {},
   "outputs": [],
   "source": [
    "#converting data frame to a dictionary\n",
    "\n",
    "import pickle\n",
    "\n",
    "edges = {}\n",
    "titles = {}\n",
    "\n",
    "for index, row in facts.iterrows():\n",
    "    edges[(row[\"head\"], row[\"tail\"])] = row[\"relation\"]\n",
    "    if (row[\"head\"], row[\"tail\"]) not in titles:\n",
    "        titles[(row[\"head\"], row[\"tail\"])] = f\"This fact has been recorded in the following patents: US{row['patent_id']}\"\n",
    "    else:\n",
    "        titles[(row[\"head\"], row[\"tail\"])] += f\", US{row['patent_id']}\"\n",
    "        \n",
    "with open(\"data/airflow-facts.pkl\", \"wb\") as f:\n",
    "    pickle.dump(edges, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "252a3db3-7dbb-4139-9d14-668fbd6e9578",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[('noise', 55),\n",
       " ('airflow', 48),\n",
       " ('airflow noise', 21),\n",
       " ('airflow path', 7),\n",
       " ('motor fan assembly', 6),\n",
       " ('fan noise', 6),\n",
       " ('serpentine airflow route', 4),\n",
       " ('noise level', 4),\n",
       " ('noise reduction plate', 4),\n",
       " ('motor cooling airflow properties', 3)]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Getting entity counts\n",
    "\n",
    "from collections import Counter\n",
    "\n",
    "entities = facts[\"head\"].tolist() + facts[\"tail\"].tolist()\n",
    "counts = Counter(entities)\n",
    "\n",
    "max_freq = counts.most_common(1)[0][1]\n",
    "\n",
    "counts.most_common(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "4028e879-c0b6-42d0-84f9-a883b08a5726",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "airflow-facts.html\n"
     ]
    }
   ],
   "source": [
    "#visualising the selected facts\n",
    "from pyvis.network import Network\n",
    "import random\n",
    "\n",
    "g = Network(width=\"100%\", height=\"1600px\", directed =True)\n",
    "\n",
    "nodes = list(set(entities))\n",
    "sizes = [(((counts[item] - 1)/(max_freq - 1))*10) + 3 for item in nodes]\n",
    "labels = [f\"This entity has been mentioned in {counts[item]} facts.\" for item in nodes]\n",
    "colors = [\"#708090\" if item in [\"noise\", \"airflow noise\"] else \"#A9A9A9\" for item in nodes]\n",
    "\n",
    "g.add_nodes(nodes, size=sizes, color=colors, title=labels)\n",
    "\n",
    "for h, t in edges:\n",
    "    #including an edge by chance to ensure the visualisation is readable\n",
    "    if random.random() < 1:\n",
    "        g.add_edge(source=h, to=t, label=edges[(h, t)], color=\"#D3D3D3\", title=titles[h, t])\n",
    "\n",
    "g.show(f\"airflow-facts.html\", notebook=False)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "d0da2908-1774-4b89-a20c-1896a94bc9da",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patent_id</th>\n",
       "      <th>sentence_id</th>\n",
       "      <th>sentence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>10001020</td>\n",
       "      <td>10001020_0</td>\n",
       "      <td>The present invention generally relates to fan...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>10001020</td>\n",
       "      <td>10001020_1</td>\n",
       "      <td>The improved fan blade is characterized by a f...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>10001020</td>\n",
       "      <td>10001020_2</td>\n",
       "      <td>The fan blade body includes opposingly paired ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>10001020</td>\n",
       "      <td>10001020_3</td>\n",
       "      <td>The fan blade/fan blade body may be a single t...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>10001020</td>\n",
       "      <td>10001020_4</td>\n",
       "      <td>As to the latter, it is advantageously contemp...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>603179</th>\n",
       "      <td>9995315</td>\n",
       "      <td>9995315_33</td>\n",
       "      <td>The ceiling fan motor housing and cover side f...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>603180</th>\n",
       "      <td>9995315</td>\n",
       "      <td>9995315_34</td>\n",
       "      <td>A ceiling fan motor housing and cover side fix...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>603181</th>\n",
       "      <td>9995315</td>\n",
       "      <td>9995315_35</td>\n",
       "      <td>The motor housing includes a rotor and plural ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>603182</th>\n",
       "      <td>9995315</td>\n",
       "      <td>9995315_36</td>\n",
       "      <td>The cover includes plural fixing holes formed ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>603183</th>\n",
       "      <td>9995315</td>\n",
       "      <td>9995315_37</td>\n",
       "      <td>Further, the wire winding space of the stator ...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>603184 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       patent_id sentence_id  \\\n",
       "0       10001020  10001020_0   \n",
       "1       10001020  10001020_1   \n",
       "2       10001020  10001020_2   \n",
       "3       10001020  10001020_3   \n",
       "4       10001020  10001020_4   \n",
       "...          ...         ...   \n",
       "603179   9995315  9995315_33   \n",
       "603180   9995315  9995315_34   \n",
       "603181   9995315  9995315_35   \n",
       "603182   9995315  9995315_36   \n",
       "603183   9995315  9995315_37   \n",
       "\n",
       "                                                 sentence  \n",
       "0       The present invention generally relates to fan...  \n",
       "1       The improved fan blade is characterized by a f...  \n",
       "2       The fan blade body includes opposingly paired ...  \n",
       "3       The fan blade/fan blade body may be a single t...  \n",
       "4       As to the latter, it is advantageously contemp...  \n",
       "...                                                   ...  \n",
       "603179  The ceiling fan motor housing and cover side f...  \n",
       "603180  A ceiling fan motor housing and cover side fix...  \n",
       "603181  The motor housing includes a rotor and plural ...  \n",
       "603182  The cover includes plural fixing holes formed ...  \n",
       "603183  Further, the wire winding space of the stator ...  \n",
       "\n",
       "[603184 rows x 3 columns]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Import the sentences\n",
    "\n",
    "sentences = pd.read_pickle(\"data/fan-systems-sentences.pkl\")\n",
    "sentences"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "561a1974-029b-4b10-894b-608069f830f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>patent_id</th>\n",
       "      <th>sentence_id</th>\n",
       "      <th>sentence</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>232477</th>\n",
       "      <td>11286953</td>\n",
       "      <td>11286953_208</td>\n",
       "      <td>Accordingly, it is possible to further suppres...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>329514</th>\n",
       "      <td>6217281</td>\n",
       "      <td>6217281_97</td>\n",
       "      <td>The low noise fan filter unit as recited in cl...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>15626</th>\n",
       "      <td>10100846</td>\n",
       "      <td>10100846_96</td>\n",
       "      <td>The fan assembly of claim 2 in which the noise...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>101547</th>\n",
       "      <td>10537041</td>\n",
       "      <td>10537041_25</td>\n",
       "      <td>Accordingly, by means of the heat dissipation ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>248526</th>\n",
       "      <td>11371525</td>\n",
       "      <td>11371525_51</td>\n",
       "      <td>After the first blade 121 at the inlet of the ...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>...</th>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "      <td>...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>71311</th>\n",
       "      <td>10400785</td>\n",
       "      <td>10400785_50</td>\n",
       "      <td>By being able to produce the same airflow at a...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>505558</th>\n",
       "      <td>9011092</td>\n",
       "      <td>9011092_9</td>\n",
       "      <td>As a result, an airflow in a region downstream...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>602942</th>\n",
       "      <td>9995311</td>\n",
       "      <td>9995311_149</td>\n",
       "      <td>The concave portion CRC may guide airflow conc...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>329532</th>\n",
       "      <td>6217281</td>\n",
       "      <td>6217281_115</td>\n",
       "      <td>A low noise fan filter unit for providing filt...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>545162</th>\n",
       "      <td>9528374</td>\n",
       "      <td>9528374_14</td>\n",
       "      <td>The present invention can suppress the separat...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>559 rows × 3 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "       patent_id   sentence_id  \\\n",
       "232477  11286953  11286953_208   \n",
       "329514   6217281    6217281_97   \n",
       "15626   10100846   10100846_96   \n",
       "101547  10537041   10537041_25   \n",
       "248526  11371525   11371525_51   \n",
       "...          ...           ...   \n",
       "71311   10400785   10400785_50   \n",
       "505558   9011092     9011092_9   \n",
       "602942   9995311   9995311_149   \n",
       "329532   6217281   6217281_115   \n",
       "545162   9528374    9528374_14   \n",
       "\n",
       "                                                 sentence  \n",
       "232477  Accordingly, it is possible to further suppres...  \n",
       "329514  The low noise fan filter unit as recited in cl...  \n",
       "15626   The fan assembly of claim 2 in which the noise...  \n",
       "101547  Accordingly, by means of the heat dissipation ...  \n",
       "248526  After the first blade 121 at the inlet of the ...  \n",
       "...                                                   ...  \n",
       "71311   By being able to produce the same airflow at a...  \n",
       "505558  As a result, an airflow in a region downstream...  \n",
       "602942  The concave portion CRC may guide airflow conc...  \n",
       "329532  A low noise fan filter unit for providing filt...  \n",
       "545162  The present invention can suppress the separat...  \n",
       "\n",
       "[559 rows x 3 columns]"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#Find sentences that include the issue term\n",
    "\n",
    "sentences = sentences.loc[sentences[\"sentence\"].str.contains(r'^(?=.*airflow)(?=.*noise)')]\n",
    "sentences.sample(frac=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2aec7fdd-5130-49b6-9353-04b6f0700ee6",
   "metadata": {},
   "outputs": [],
   "source": [
    "#getting the list of sentences as text file\n",
    "\n",
    "with open(\"data/airflow-sentences.txt\", \"w+\", encoding=\"utf-8\") as f:\n",
    "    f.write(\"\\n\\n\".join(sentences[\"sentence\"].tolist()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0be6b668-997b-4eb1-adec-cf931a23508e",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:base] *",
   "language": "python",
   "name": "conda-base-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
